package yxy.game.pm2.active.module.sdk.weixin;

import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.active.module.ActOpenHandle;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.OpenType;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.sdk.WeiXinOtherParm;

import java.util.ArrayList;

public class ActiveModule_武将分享 extends PlayerModule {

	//bean -----------------------------------------------------------	
	public static final int acttype = 203;
	
	private ArrayList<Integer> shareheroids = new ArrayList<Integer>();	//已经分享的武将id 五星
 	
	public ArrayList<Integer> getShareheroids() {
		return shareheroids;
	}
	public void setShareheroids(ArrayList<Integer> shareheroids) {
		this.shareheroids = shareheroids;
	}

	//func ------------------------------------------------------------
	
//	public static final int canget = 1;
//	public static final int hadget = 2;
//	public static final int cannotget = 3;

	
	/** 创建实例 **/
	private static ActiveModule_武将分享 create(String playerCode) {
		ActiveModule_武将分享 am = new ActiveModule_武将分享();
		am.setPlayerCode(playerCode);
		am.set();
		return am;
	}

	/**
	 * 能否分享
	 * @param goodsid
	 * @return
	 */
	public int canbuy(int heroid) {
		if(getShareheroids().contains(heroid)) {
			return DrawStatus.hadget.getStatus();
		}
		return DrawStatus.canget.getStatus();
	}	
	
	//redis -----------------------------------------------
	
	/** 获取redis key **/
	static private String key(String playerCode) {
		return String.format("PLAYER:%s:ACTIVE:%S", playerCode, acttype);
	}

	/** 保存 **/
	public void set() {
		try {
			setv(key(getPlayerCode()), this);
		} catch (RedisException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取ActiveModule_武将分享
	 * @param playerCode
	 * @return
	 */
	static public ActiveModule_武将分享 get(String playerCode) {
		try {
			PlayerBean pb = PlayerBean.get(playerCode);
			WeiXinOtherParm wxop = WeiXinOtherParm.get(pb.getUid());
			if(wxop == null) {
				return null;
			}
			
			ActiveModule_武将分享 module = getv(key(playerCode));
			if (module == null) {
				module = create(playerCode);
			}
			module.check();
			return module;
		} catch (RedisException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/** 检查红点，活动是否开启 **/
	public void check() {
		new ActOpenHandle().updateActOpen(getPlayerCode(), acttype, OpenType.开启);
	}
	
	//lock --------------------------------------------------------
	@Override
	public String lockKey() {
		return String.format("lock:%s", key(getPlayerCode()));
	}

}
